package com.hsc.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hsc.eduservice.entity.EduChapter;
import com.hsc.eduservice.entity.EduVideo;
import com.hsc.eduservice.entity.chapter.ChapterVo;
import com.hsc.eduservice.entity.chapter.VideoVo;
import com.hsc.eduservice.mapper.EduChapterMapper;
import com.hsc.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsc.eduservice.service.EduVideoService;
import com.hsc.servicebase.exceptionHandle.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-13
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //根据课程id查询所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperChapter);
        //根据课程id查询所有小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideos = videoService.list(wrapperVideo);
        //创建list集合用于封装最后的结果数据
        List<ChapterVo> finalList = new ArrayList<>();
        //遍历查询章节list集合进行封装
        for (int i = 0; i < eduChapters.size(); i++) {
            ChapterVo chapterVo = new ChapterVo();
            EduChapter eduChapter = eduChapters.get(i);
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalList.add(chapterVo);
            //遍历小节list集合，进行封装
            List<VideoVo> videoVoList = new ArrayList<>();
            for (int n = 0 ; n < eduVideos.size() ; n++){
                EduVideo eduVideo = eduVideos.get(n);
                if(eduVideo.getChapterId().equals(chapterVo.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
        }
        return finalList;
    }

    //删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id查询小节数据，如果存在小节数据则不进行章节删除操作
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("chapter_id",chapterId);
//        List<EduVideo> list = videoService.list(wrapperVideo);
        //统计章节下的小节数据
        int count = videoService.count(wrapperVideo);
        if(count>0){//查询出数据，不进行删除
            throw new GuliException(20001,"章节下有小节未删除，不能进行删除操作");
        }else {//未查询出小节数据，进行删除操作
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }

    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
